import {URL, connection, express} from './_comm'

const router = express.Router();
const sql = 'SELECT * FROM users';
const addSql = 'INSERT INTO users(name,sex) VALUES(?,?)';

/* GET users listing. */
router.get('/', (req, res, next) => {
  res.send('respond with a resource');
});

/* GET home page. */
router.get('/add', (req, res, next) => {
  var params = URL.parse(req.url, true).query;
  let addSqlParams = [params.name, params.sex];
  //增
  connection.query(addSql, addSqlParams, (err, result) => {
    console.log('result', result)
    if (err) {
      console.log('[INSERT ERROR] - ', err.message);
      res.json({"success": false});
    } else {
      res.json({"success": true});
    }
  });
});

/* GET home page. */
router.get('/list', (req, res, next) => {
  let params = URL.parse(req.url, true).query;
  connection.query(sql, (err, result) => {
    if (err) {
      console.log('[SELECT ERROR] - ', err.message);
      return;
    }
    console.log(params.id);
    //把搜索值输出
    res.json({"success": true, data: result});
  });
});

module.exports = router;
